package com.zshy.base.monitor.domain.sender.factory;

import com.zshy.base.core.contract.enums.ResultCodeEnum;
import com.zshy.base.monitor.domain.contract.dto.StarMonitorDTO;
import com.zshy.base.monitor.domain.contract.enums.MonitorEnum;
import com.zshy.base.monitor.domain.contract.exception.MonitorException;
import com.zshy.base.monitor.domain.sender.factory.impl.DingDingRobotSender;
import com.zshy.base.monitor.domain.sender.factory.impl.QiWeiRobotSender;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.Objects;

/**
 * 监控发送工厂
 *
 * @author zhouhengzhe
 */
@Slf4j
@RequiredArgsConstructor
public class MonitorSendFactory {

    private final Map<String, Sender> senderMap;

    /**
     * 通过yml方式注入bean
     *
     * @param monitorEnum
     * @param <T>
     * @return
     */
    public <T extends Sender> T getSender(MonitorEnum monitorEnum) {
        Sender sender = senderMap.get(monitorEnum.getCode());
        if (sender != null) {
            return (T) sender;
        }
        return null;
    }

    /**
     * 通过数据方式自由注入bean
     *
     * @param monitorEnum
     * @param starMonitorDTO 配置信息
     * @return
     */
    public Sender getSender(MonitorEnum monitorEnum, StarMonitorDTO starMonitorDTO) {
        if (MonitorEnum.DING_TASK.equals(monitorEnum)) {
            if (Objects.isNull(starMonitorDTO.getDingTalkDTO())) {
                log.warn("monitorEnum is DING_TASK, but dingTalkDTO is null");
                throw new MonitorException(ResultCodeEnum.PARAM_NULL);
            }
            return new DingDingRobotSender(starMonitorDTO.getDingTalkDTO().getToken(), starMonitorDTO.getDingTalkDTO().getSecret());
        } else if (MonitorEnum.QI_WEI.equals(monitorEnum)) {
            if (Objects.isNull(starMonitorDTO.getQiWeiDTO())) {
                log.error("monitorEnum is ENTERPRISE_WECHAT, but qiWeiDTO is null");
                throw new MonitorException(ResultCodeEnum.PARAM_NULL);
            }
            return new QiWeiRobotSender(starMonitorDTO.getQiWeiDTO().getKey());
        }
        throw new MonitorException(ResultCodeEnum.MEDIA_TYPE_EX);
    }
}
